package com.whitehot.klib.engine;

import android.app.Activity;
import android.os.SystemClock;
import android.widget.Toast;


/**
 * Created by ytf on 2017/8/14 014.
 * Description: 双击退出
 */

public class DoubleClickExit
{
    Activity ac;
    public DoubleClickExit(Activity a)
    {
        ac = a;
    }

    long[] bf = new long[2];

    public void onBackPressed()
    {
        System.arraycopy(bf, 1, bf, 0, 1);
        bf[1] = SystemClock.uptimeMillis();
        if(bf[1] - bf[0] < 2000)
        {
            if(l != null)
            {
                l.onExit();
            }
            ac.finish();
        }else
        {
            Toast.makeText(ac, "再按一次退出", Toast.LENGTH_SHORT).show();
        }
    }

    private OnExitListener l;

    public void setOnExitListener(OnExitListener ll)
    {
        l = ll;
    }

    /***
     * 退出监听，用于释放资源
     */
    public interface OnExitListener
    {
        void onExit();
    }
}